package com.lex.newbasketball.testhandler;

import android.content.Intent;
import android.os.AsyncTask;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.lex.newbasketball.R;
import com.sam.ui.base.activity.BaseActivity;

/**
 * Created by Administrator on 2016/8/2.
 */
public class AsyncTaskActivity extends BaseActivity{
    private EditText et;
    private Button btn;

    @Override
    protected void onCreate(@Nullable Intent intent) {
        setContentView(R.layout.activity_async_task);
    }

    @Override
    protected void findView() {
        et = (EditText) findViewById(R.id.et);
        btn = (Button) findViewById(R.id.btn);
    }

    @Override
    protected void initView() {
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new MyAsyncTask().execute(10);
            }
        });
    }

    class MyAsyncTask extends AsyncTask< Integer, String, String>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            btn.setText("");
        }

        @Override
        protected String doInBackground(Integer... params) {
            for (int i = 10 ; i > 0; i--){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(String string) {
            super.onPostExecute(string);
            btn.setText("倒计时完成");
        }

        @Override
        protected void onProgressUpdate(String... values) {
            super.onProgressUpdate(values);
            btn.setText("" + values[0]);
        }
    }
}
